package com.feiyuan.common.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 加密、解密
 */
public class Sha256Utils {

    private static final String salt = "feiyuan-start";

    /**
     * sha256加密
     * @param str
     * @return
     */
    public static String encryption(String str) {
        try {
            // 创建SHA256的MessageDigest实例
            MessageDigest digest = MessageDigest.getInstance("SHA-256");

            // 加盐后的字符串
            String salt_str = salt + str;
            // 计算哈希值
            byte[] hash = digest.digest(salt_str.getBytes());

            // 显示哈希值的十六进制表示
            StringBuilder hexString = new StringBuilder();
            for (byte b: hash) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) hexString.append('0');
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解密
     */
    public static Boolean decryption(String str) {

        return null;
    }

//    public static void main(String[] args) {
//        System.out.println(Sha256Utils.encryption("admin"));
//    }
}
